

/*
summary stats and plots for auction data

output:
- t1b_*.tex: tables with various auction summary stats; combined with python
- f1b_D_shock_timeseries*.pdf: figure with shock time series
- f1b_b2c_binscatter_govpx_cme_shocks.pdf: figure comaring govpx/cme shocks
- f1b_rolling_sd_shock.pdf: rolling std dev of shocks
*/


* plotting options
set scheme lean_uncluttered


********************************************************************************
********************************************************************************
* shock summary stats
n di "summary stats: auction shocks"

use ../data/auction_notesbonds_govpx_shock.dta, clear
* recession and ZLB indicator
rename auctionDate date
merge m:1 date using ../data/fred/fred_daily_shocks.dta, ///
	nogen keep(match) keepusing(USRECD ZLB)

* create labels
* note: use "\( ... \)" for math mode, rather than "$ .. $"
* will show up ugly in png files but nicely in latex
* math-mode in tex will ignore extra space
label var D_shock "\( D_t \)"
label define auc_terms 2 "\( D_t^{(2Y)} \)" 3 "\( D_t^{(3Y)} \)" ///
	5 "\( D_t^{(5Y)} \)" 7 "\( D_t^{(7Y)} \)" ///
	10 "\( D_t^{(10Y)} \)" 30 "\( D_t^{(30Y)} \)" 
label values term_group auc_terms

label define ZLB_periods 0 "Non-ZLB" 1 "ZLB"
label values ZLB ZLB_periods
label define USRECD_periods 0 "Expansion" 1 "Recession"
label values USRECD  USRECD_periods


* tables
eststo clear
* for generating labels in output tables
local table_stats mean p50 sd min max count
local table_cell_str mean(fmt(2) label(Mean)) p50(fmt(2) label(Med.)) ///
	sd(fmt(2) label(Std. Dev.)) min(fmt(2) label(Min)) ///
	max(fmt(2) label(Max)) ///
	count(fmt(0) label(Obs.)) 

* save to tex (note: keep underscores without slash)

* all auctions, all terms
eststo r_summ_all: estpost tabstat D_shock, ///
	statistics(`table_stats') columns(statistics)
esttab r_summ_all using ../tables/t1b_shock_summary_all.tex, replace ///
	cells("`table_cell_str'") noobs label nonumbers substitute(\_ _)

* auctions by terms
eststo r_summ_term: estpost tabstat D_shock, ///
	statistics(`table_stats') columns(statistics) by(term_group) nototal
esttab r_summ_term using ../tables/t1b_shock_summary_terms.tex, replace ///
	cells("`table_cell_str'") noobs label nonumbers substitute(\_ _)

* auctions by ZLB
eststo r_summ_ZLB: estpost tabstat D_shock, ///
	statistics(`table_stats') columns(statistics) by(ZLB) nototal
esttab r_summ_ZLB using ../tables/t1b_shock_summary_ZLB.tex, replace ///
	cells("`table_cell_str'") noobs label nonumbers substitute(\_ _)

* auctions by recession
eststo r_summ_rec: estpost tabstat D_shock, ///
	statistics(`table_stats') columns(statistics) by(USRECD) nototal
esttab  r_summ_rec using ../tables/t1b_shock_summary_rec.tex, replace ///
	cells("`table_cell_str'") noobs label nonumbers substitute(\_ _)


*****************
* compare with non-auction dates
n di "summary stats: non-auction shocks"

use ../data/nonauction_notesbonds_govpx_shock.dta, clear
* only keep if enough observations within window
drop if N_obs_pre<=5 | N_obs_post<=5
drop if mm(time_pre)!=45 | mm(time_post)!=10

* create labels
label var D_shock "\( \tilde{D}_t \)"
label define auc_terms 2 "\( \tilde{D}_t^{(2Y)} \)" 5 "\( \tilde{D}_t^{(5Y)} \)" ///
	10 "\( \tilde{D}_t^{(10Y)} \)" 30 "\( \tilde{D}_t^{(30Y)} \)" 
label values term_group auc_terms

* non-auctions by terms
eststo r_nonauc_term: estpost tabstat D_shock, ///
	statistics(`table_stats') columns(statistics) by(term_group) nototal
esttab r_nonauc_term using ../tables/t1b_nonauction_summary_terms.tex, replace ///
	cells("`table_cell_str'") noobs label nonumbers substitute(\_ _)



*********************************
* formal test of non-zero mean (not saved to table)
ttest D_shock == 0
n di "T-test pvalue, full sample: ", r(p)
levelsof term_group, local(term_groups)
foreach t of local term_groups {
	ttest D_shock == 0 if term_group == `t'
	n di "T-test pvalue, term=`t': ", r(p)
}


********************************************************************************
********************************************************************************
* time series of shocks
n di "shock time series"

use ../data/auction_notesbonds_govpx_shock.dta, clear
* year-only date format for plots
format auctionDate %tdCY
sort auctionDate cusip

* date range: 1995 to 2017; keep axis tight
local xmin `=mdy(1,1,1995)'
local xmax `=mdy(12,31,2017)'
local xlabels1 `=mdy(1,1,1995)' `=mdy(1,1,2000)' `=mdy(1,1,2005)' ///
	`=mdy(1,1,2010)' `=mdy(1,1,2015)' 
local xlabels2 `=mdy(1,1,1995)' `=mdy(1,1,2005)' `=mdy(1,1,2015)'


* all shocks
twoway line D_shock auctionDate, xtitle("") ytitle("D{sub:t} Shock (b.p.)") ///
	xscale(r(`xmin' `xmax')) xlabel(`xlabels1') nodraw

* display with correct size and export
local fname f1b_D_shock_timeseries
graph rename Graph `fname', replace
graph display `fname', xsize(8) ysize(5)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace


* insert a "missing" value for when 3 and 30y were not issued
* to make sure plot does not connect lines before and after
set obs `=_N+1'
replace term_group = 3 in `=_N'
replace auctionDate = mdy(1, 1, 2001) in `=_N'
set obs `=_N+1'
replace term_group = 30 in `=_N'
replace auctionDate = mdy(1, 1, 2003) in `=_N'
sort auctionDate cusip

* time series by term
levelsof term_group, local(mat_groups)
foreach m of local mat_groups {
	n di "`m'"
	local y_axis_str ytitle("D{sub:t}{sup:(`m'Y)} Shock (b.p.)")
	*local y_axis_str ytitle("\(D_t^{(`m'Y)}\) (b.p.)")
	*local y_axis_str ytitle("")
	
	twoway line D_shock auctionDate if term_group==`m', ///
		cmissing(n) xtitle("") `y_axis_str' title("`m'-Year") ///
		xscale(r(`xmin' `xmax')) xlabel(`xlabels2') ///
		name("D_shock_mat`m'", replace) nodraw
}
* combine with correct size and export 
local fname f1b_D_shock_timeseries_by_term
graph combine D_shock_mat2 D_shock_mat3 D_shock_mat5 ///
	D_shock_mat7 D_shock_mat10 D_shock_mat30 , ///
	rows(3) imargin(tiny) xsize(5) ysize(7) name(`fname', replace)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace


*****************
* compare with futures
n di "compare govpx and cme shocks"
use ../data/auction_notesbonds_govpx_shock.dta, clear
rename cusip auction_cusip
rename auctionDate date
merge 1:1 auction_cusip date using ../data/input/cme_shock_cls.dta, ///
	nogen keep(master match)

* compare, by futures contracts
* get correct auction terms for each futures contract
local futures_tics TU FV TYF US
local futures_years 2 5 10 30
gen term_groupTU = term_group==2
gen term_groupFV = term_group==3 | term_group==5
gen term_groupTYF = term_group==7 | term_group==10
gen term_groupUS = term_group==30

forvalues i=1/4 {
	* only label left/bottom figures
	if `i'==1 | `i'==3 local y_axis_str ytitle("Futures Price Shock")
	else local y_axis_str ytitle("")
	if `i'==3 | `i'==4 local x_axis_str xtitle("Spot Yield Shock")
	else local x_axis_str xtitle("")
	
	local tic: word `i' of `futures_tics'
	local y: word `i' of `futures_years'
	binscatter D_shock`tic' D_shock if term_group`tic', msymbol(O) ///
		colors(black black) name(cme`y'y, replace) ///
		`x_axis_str' `y_axis_str' title("`y'-Year Contract") nodraw
}
* combine with correct size and export
local fname f1b_binscatter_govpx_cme_shocks
graph combine cme2y cme5y cme10y cme30y, ///
	rows(2) imargin(tiny) xsize(8) ysize(5) name(`fname', replace)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace




********************************************************************************
********************************************************************************
* rolling std dev of shocks
n di "rolling shocks"
use ../data/auction_notesbonds_govpx_shock.dta, clear
* time variable: auction
sort close_time
gen _T = _n
tsset _T
* note: rolling saves auctionDate as date
rolling sd_shock=r(sd) N_shock=r(N), window(50) keep(auctionDate) ///
	saving(../data/rolling/sd_shock.dta, replace) : ///
	summ D_shock

* plot rolling std dev
use ../data/rolling/sd_shock.dta, clear
* year-only date format for plots
format date %tdCY
* date range: 1995 to 2017; keep axis tight
local xmin `=mdy(1,1,1995)'
local xmax `=mdy(12,31,2017)'
local xlabels `=mdy(1,1,1995)' `=mdy(1,1,2000)' `=mdy(1,1,2005)' ///
	`=mdy(1,1,2010)' `=mdy(1,1,2015)' 
twoway line sd_shock date , ///
	ytitle("Shock Std. Dev.") xtitle("") ///
	xscale(r(`xmin' `xmax')) xlabel(`xlabels') nodraw

* display with correct size and export
local fname f1b_rolling_sd_shock
graph rename Graph `fname', replace
graph display `fname', xsize(8) ysize(5)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace





